package com.wanshi.servlet;

import com.wanshi.dao.UserDao;
import com.wanshi.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findUser")
public class FindServlet extends HttpServlet {

    private UserDao userDao = new UserDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        //1 获取用户名
        String username = request.getParameter("username");

        //2 导数据库查找
        User user = userDao.findUserByName(username);



        //3  根据是否存在输出不同内容
        if(user == null){
            //不存在
            response.getWriter().write("<span style='color:green;'>用户名可以使用</span>");
        }else{
            //存在
            response.getWriter().write("<span style='color:red;'>用户名已经被注册</span>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

